package com.zeng.generate.code.util;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public class CommonUtil {

    public static int getWeekNumber(Date date) {
        // 当前星期几
        Integer[] weekDays = {7, 1, 2, 3, 4, 5, 6};
        return weekDays[(DateUtil.dayOfWeek(date) - 1)];
    }

    public static BigDecimal betweenHour(Date beginTime, Date endTime){
        if (ObjectUtil.isNotNull(beginTime) && ObjectUtil.isNotNull(endTime)) {
            Long datePoor = DateUtil.between(beginTime, endTime, DateUnit.MINUTE);
            BigDecimal hour = new BigDecimal(datePoor.doubleValue()/60);
            return hour.setScale(2, BigDecimal.ROUND_HALF_UP);
        }
        return new BigDecimal(0);
    }

    public static List pageList(List list, Integer pageNum, Integer pageSize) {
        if (list == null || list.isEmpty()) {
            return null;
        }

        Integer count = list.size(); // 记录总数
        Integer pageCount = 0; // 页数
        if (count % pageSize == 0) {
            pageCount = count / pageSize;
        } else {
            pageCount = count / pageSize + 1;
        }

        int fromIndex = 0; // 开始索引
        int toIndex = 0; // 结束索引

        if (!pageNum.equals(pageCount)) {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = fromIndex + pageSize;
        } else {
            fromIndex = (pageNum - 1) * pageSize;
            toIndex = count;
        }
        return list.subList(fromIndex, toIndex);
    }

}
